home *** CD-ROM | disk | FTP | other *** search
- /*
- Program: Lamp.c
-
- Purpose: Tutorial demonstration -- Lamp on Table
-
- ©1991 Mark M. Owen -- All Rights Reserved.
- */
-
- /* Compiler specific headers
- */
- #pragma segment Main
-
- #ifndef THINK_C
-
- #include <OSEvents.h>
- #include <Fonts.h>
- #include <Desk.h>
- #include <Dialogs.h>
- #include <Menus.h>
- #include <Memory.h>
- #include <Packages.h>
- #include <Quickdraw.h>
- #include <Resources.h>
- #include <Traps.h>
- #include <Windows.h>
-
- extern _DataInit();
-
- #endif THINK_C
-
- /* 3d Graphic Tools headers (for things we'll use only)
- */
- #include <3dExterns.h>
- #include <3dGrid.h>
- #include <3dHermiteCurves.h>
- #include <3dHierarchy.h>
- #include <3dResources.h>
- #include <3dMatrix2Patch.h>
- #include <3dQuadratics.h>
- #include <3dSolidsPgn.h>
- #include <3dSpline2Patch.h>
- #include <3dText.h>
- #include <Camera.h>
- #include <OffScreenPorts.h>
- #include <SysEnvirons.h>
-
-
- /* define this to observe the offscreen drawing effects
- */
- #undef _OFFSCREEN_
-
-
- /* Define resource related items
- */
- #define RESOURCEFILE "\pLamp.π.rsrc"
- #define WINDOWID 1001
-
- /* Define a few, miscellaneous, fixed point constants
- */
- #define f_0p1 6554L
- #define f_0p4 26214L
- #define f_0p6 39322L
- #define f_3p6 235930L
- #define f_6 393216L
-
- /* Prototypes for our functions... very important
- */
- void main (void);
- void ProduceScene (void);
- void DrawGrid (void);
- hCollection MakeLamp (void);
- void GenRotatedSurfaceBody (hGroup hG,short ixP);
- void GenRotatedSurfaceTable (hGroup hG,short ixP);
- void SetRenderingAttributes (pRendAttr pRA,colorFactor r,colorFactor g,colorFactor b);
- pLighting SetupLighting (void);
-
-
- void main()
- {
- short masters = 4;
- EventRecord evt; /* for event polling */
- WindowPtr wp;
-
- #ifndef THINK_C
- UnloadSeg(_DataInit); /* for MPW C only, get rid of Data Initializer after use */
- #endif
-
- MaxApplZone();
- while( masters-- )
- MoreMasters();
- FlushEvents(everyEvent, 0);
- #ifndef THINK_C
- InitGraf(& QDGLOBALS thePort);
- #else
- InitGraf(&thePort);
- #endif THINK_C
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
-
- OpenResFile( RESOURCEFILE );
- SelectWindow( (wp = GetNewCWindow( WINDOWID, (Ptr)0L, (WindowPtr)-1L )) );
- SetPort( wp );
-
- Init3d();
-
- ProduceScene();
-
- while( !GetNextEvent( mDownMask|keyDownMask, &evt ) );
-
- FlushEvents(everyEvent, 0); /* get rid of any leftovers */
- DisposeWindow( FrontWindow() ); /* don't need this anymore */
- ExitToShell(); /* we are out of here... NOW! */
- }
-
- void ProduceScene()
- {
- hCollection hC; /* collection storage reference */
- Point3d Camera = {-f_100,f_45,-f_180};/* camera location */
- Point3d Focus = {f_0,f_0,f_0}; /* camera focus point */
- Fixed Lens = Lens80mm; /* angle of view / magnification */
- pLighting pL; /* lighting information */
- WindowPtr theWindow = FrontWindow();
- GrafPtr osGP;
-
- /* Generate the scene objects
- */
- hC = MakeLamp();
-
- AimCamera3d( Camera.x,Camera.y,Camera.z, Focus.x,Focus.y,Focus.z, Lens );
-
- /* Setup ambient and point light sources
- */
- pL = SetupLighting();
-
- #ifdef _OFFSCREEN_
- /* Create offscreen drawing port
- */
- if( ColorPresent )
- {
- osGP = (GrafPtr)NewOffScreenCPort( &theWindow->portRect );
- UseOffScreenCPort( (CGrafPtr)osGP );
- }
- else
- {
- osGP = NewOffScreenPort( &theWindow->portRect );
- UseOffScreenPort( osGP );
- }
- #endif _OFFSCREEN_
-
- /* Clear the screen
- */
- EraseRect( &theWindow->portRect );
-
- DrawGrid();
-
- /* Decide how to render it based on the graphics device bit depth
- */
- if( GDevicePixelBits() > 8 )
- { /* We have a VERY good screen so use ZBuffered rendering method
- */
- SetAntiAliasing(true);
- xfRenderCollectionPgn( hC, &xFormViewer, pL );
- }
- else
- /* We have only a basic mono, grayscale or 8 bit color screen
- * so use the Paint rendering method
- */
- xfRenderCollection( hC, &xFormViewer, pL, false );
-
- /* release some of the memory we allocated
- */
- DisposeCollection( hC );
- DisposPtr( (Ptr)pL );
-
- #ifdef _OFFSCREEN_
- /* Display the offscreen buffer and dispose of it
- */
- if( ColorPresent )
- {
- ShowOffScreenCPort( (CGrafPtr)osGP, theWindow );
- DisposeOffScreenCPort( (CGrafPtr)osGP );
- }
- else
- {
- ShowOffScreenPort( osGP, theWindow );
- DisposeOffScreenPort( osGP );
- }
- #endif _OFFSCREEN_
- }
-
- void DrawGrid(void)
- {
- Point3d min = { -f_16,-f_16,-f_16 };
- Point3d max = { f_16, f_16, f_16 };
- Point3d steps = { f_8,f_8,f_8 };
- GridOptions options;
-
- options.left = false;
- options.right = true;
- options.top = false;
- options.bottom = true;
- options.front = false;
- options.back = true;
- options.gridPat = GP_BLACK;
- options.connectX= false;
- options.connectY= false;
- options.connectZ= false;
- ForeColor( blueColor );
- xfGrid( min, max, steps, options, &xFormViewer );
- ForeColor( blackColor );
- xFormCombined = xFormViewer;
- TextFace( bold );
- TextFont( geneva );
- TextSize( 12 );
- DrawString3d(min.x,f_0,max.z,"\pX",centerAlign);
- DrawString3d(f_0,max.y,max.z,"\pY",centerAlign);
- DrawString3d(f_0,min.y,min.z,"\pZ",centerAlign);
- }
-
- hCollection MakeLamp()
- {
- hCollection hC; /* collection storage reference */
- hGroup hG; /* group storage reference */
- RendAttr RA; /* working copy of rendering attributes */
-
- hC = NewCollection( 2 );/* allocate storage for collection */
- hG = NewGroup( 4 ); /* ditto for the group and patches */
-
- (**hC).hG[0] = hG; /* put group handle in collection */
-
- /* Make the lamp body
- */
- GenRotatedSurfaceBody( hG, 0 );
-
- /* Now a cylinder for the bulb support
- */
- SetRenderingAttributes( &RA, 32767,32767,32767 );
- SetInstanceReferenceFrame(f_0,f_0,f_0,f_1,f_1,f_1,f_0,Int2Fix(12),f_0);
- SolidCylinder( 8,f_0p25,f_0p25,Int2Fix(12),f_8,false,hG,1,RA,&xFormInstance);
-
- /* The light bulb itself
- */
- SetRenderingAttributes( &RA, 65535,65535,65535 );
- SetInstanceReferenceFrame(f_0,f_0,f_0,f_2,f_2+f_0p5,f_2,f_0,Int2Fix(14),f_0);
- SolidSphere(9,9,f_1,Int2Fix(9),false,hG,2,RA,&xFormInstance);
-
- /* And the lamp's shade
- */
- SetRenderingAttributes( &RA, 42000,42000,42000 );
- RA.transparency = f_0p25;
- RA.framed = true;
- SetInstanceReferenceFrame(f_0,f_0,f_0,f_1,f_1,f_1,f_0,Int2Fix(12),f_0);
- SolidCylinder( 8,f_3,f_10,Int2Fix(12),f_8,false,hG,3,RA,&xFormInstance);
-
- /* A table to set it on
- */
- hG = NewGroup( 1 ); /* allocate the group and patches */
- (**hC).hG[1] = hG; /* put group handle in collection */
- GenRotatedSurfaceTable(hG,0);
-
- return hC;
- }
-
- void GenRotatedSurfaceBody(hGroup hG,short ixP)
- {
- int nDataPts = 8;
- Point3d DataPt[] =
- {
- {f_1,f_0,f_0}
- , {f_2,f_1,f_0}
- , {f_3,f_3,f_0}
- , {f_2,f_4,f_0}
- , {f_2,f_5,f_0}
- , {f_1,f_6,f_0}
- , {f_0,f_4,f_0}
- , {f_0,f_2,f_0}
- };
- Point3d axis = {f_0,f_1,f_0}; /* rotation axis */
- RendAttr RA; /* rendering attributes */
-
- int nPts;
- Point3d *pPts;
-
- HermiteCurve3d(HERMITE_CLOSEDCURVE,3,nDataPts,DataPt,&nPts,&pPts);
- SetInstanceReferenceFrame(f_0,f_0,f_0,f_2,f_3,f_2,f_0,-Int2Fix(9),f_0);
- SetRenderingAttributes( &RA, 32767,32767,42000 );
- RotateToSurface
- ( f_0, f_360, axis, 12,
- nPts, pPts,
- hG,ixP,RA, &xFormInstance
- );
- DisposPtr( (Ptr)pPts );
- }
-
- void GenRotatedSurfaceTable(hGroup hG,short ixP)
- {
- int nDataPts = 5;
- Point3d DataPt[] =
- {
- {f_0 ,-f_0p6,f_0}
- , {f_3p6,-f_0p6,f_0}
- , {f_3p6,-f_0p4,f_0}
- , {f_3 , f_0 ,f_0}
- , {f_0 , f_0 ,f_0}
- };
- Point3d axis = {f_0,f_1,f_0}; /* rotation axis */
- RendAttr RA; /* rendering attributes */
-
- SetInstanceReferenceFrame(f_0,f_0,f_0,f_6,f_2,f_6,f_0,-Int2Fix(9),f_0);
- #undef _WOOD_
- #ifdef _WOOD_
- SetRenderingAttributes( &RA, 58000,36000,18000 );
- RA.framed = true;
- RA.frameColor.red = 65000;
- RA.frameColor.green = 57000;
- RA.frameColor.blue = 18000;
- RA.normalVisibility = true;
- RA.texture = TX_WOOD;
- SetPt3d( &RA.tx[TXI_WOOD].txRotations, Int2Fix(85),Int2Fix(30),f_0 );
- SetPt3d( &RA.tx[TXI_WOOD].txScalars, f_2+f_0p5,f_2+f_0p5,f_2+f_0p5 );
- SetPt3d( &RA.tx[TXI_WOOD].txTranslations, f_0,f_0,f_0 );
- RA.tx[TXI_WOOD].txArgument[0] = 0; /* do not add noise values */
- RA.tx[TXI_WOOD].txArgument[1] = f_0p6; /* lightest grain */
- RA.tx[TXI_WOOD].txArgument[2] = f_0p5; /* darkest grain */
- RA.tx[TXI_WOOD].txArgument[3] = 0; /* not used for wood texture */
- #else // _ROCK_
- SetRenderingAttributes( &RA, 65000,65000,65000 );
- RA.framed = true;
- RA.frameColor.red = 65000;
- RA.frameColor.green = 47000;
- RA.frameColor.blue = 3000;
- RA.normalVisibility = true;
- RA.texture = TX_ROCK;
- SetPt3d( &RA.tx[TXI_ROCK].txRotations, Int2Fix(30),Int2Fix(0),Int2Fix(20) );
- SetPt3d( &RA.tx[TXI_ROCK].txScalars, f_1,f_1,f_1 );
- SetPt3d( &RA.tx[TXI_ROCK].txTranslations, f_45,f_0,f_0 );
- RA.tx[TXI_ROCK].txArgument[0] = fixratio(16,1);
- RA.tx[TXI_ROCK].txArgument[1] = fixratio(4,10);
- RA.tx[TXI_ROCK].txArgument[2] = fixratio(8,10);
- RA.tx[TXI_ROCK].txArgument[3] = fixratio(7,10);
- RA.diffusion = fixratio(15,100);
- RA.specIndex = 30;
- RA.specularity = fixratio(85,100);
- RA.ambience = fixratio(1,100);
- #endif _WOOD_
- RotateToSurface
- ( f_0, f_360, axis, 8,
- nDataPts, DataPt,
- hG,ixP,RA, &xFormInstance
- );
- }
-
- void SetRenderingAttributes(pRendAttr pRA,colorFactor r,colorFactor g,colorFactor b)
- {
- pRA->R = r;
- pRA->G = g;
- pRA->B = b;
- pRA->frameColor.red = 32767;
- pRA->frameColor.green = 32767;
- pRA->frameColor.blue = 32767;
- pRA->lightSource = false;
- pRA->distanceEffect = true;
- pRA->ambientEffect = true;
- pRA->normalVisibility = false;
- pRA->framed = false;
- pRA->patterned = true;
- pRA->grayScaled = false;
- pRA->texture = TX_NONE;
- pRA->diffusion = f_0p5;
- pRA->specIndex = 20;
- pRA->specularity = f_0p25;
- pRA->ambience = f_0p1;
- pRA->absorption = 0;
- pRA->translucence = 0;
- pRA->refraction = 0;
- pRA->transparency = 0;
- }
-
- pLighting SetupLighting()
- {
- pLighting pL;
- LtSource LS[3];
-
- /* create light sources
- */
- Set3dLtSource
- ( &LS[0],65535,65535,65535,f_0p1,
- Int2Fix( 0 ), Int2Fix( 14 ), Int2Fix( 0 )
- );
-
- Set3dLtSource
- ( &LS[1],32767, 32767, 32767,f_1+f_0p5,
- Int2Fix( 180 ), Int2Fix( 180 ), Int2Fix( -80 )
- );
-
- Set3dLtSource
- ( &LS[2],32767, 65535, 65535,fixratio(29,10),
- Int2Fix( -200 ), Int2Fix( 180 ), Int2Fix( 200 )
- );
-
- /* set ambient light and store point sources
- */
- pL = NewLighting(32767, 32767, 32767,fixratio(5,100), 3, LS );
-
- /* transform its coordinates into the viewer's (camera's) reference frame
- */
- TransformLighting(pL,&xFormViewer);
-
- /* apply the ambient light color to the background (if we have color)
- */
- if( ColorPresent )
- RGBBackColor( &pL->color );
- return pL;
- }
-